const widgets = this.getApiPage().getApiWidgetByAliasName('三维组件');
const videoPageWidgets = this.getApiPage().getApiWidgetByAliasName('视频分页组件');
// 获取变量
var stream_url = this.getApiPage().getVariables().find(x => x.name == "stream_url").value;
const devices = this.pageObj.getApiWidgetByAliasName('devices');
const { dataSource } = devices.widgetObj._option
// const api = 'http://192.168.1.100:9098/basics-main/system/equipmentObject/listDetailByEquipmentCode'
// const api = '/Basics/basics-main/system/equipmentObject/listDetailByEquipmentCode'
const api = '/Basics/basics-main/system/equipmentObject/listDetailByEquipmentCodeByOnlineStatus'
let params = { ...$event }

console.log('dataSource: => ', dataSource)
console.log('params: => ', params)

function createCamera(camera) {
    const { equipmentConfig, equipmentName } = camera
    const _camera = { ...camera, title: equipmentName }

    if (equipmentConfig && equipmentConfig?.gbVideoConfig && equipmentConfig?.equipmentUnifyConfig) {
        const { onlineStatus } = equipmentConfig?.equipmentUnifyConfig
        const { sipUser, videoChannelId, sipServerHost, sipServerPort } = equipmentConfig.gbVideoConfig
        _camera.stream_path = `${sipUser}/${videoChannelId}`
        _camera.channelId = videoChannelId
        _camera.deviceId = sipUser
        _camera.onlineStatus = onlineStatus
        _camera.host = `${sipServerHost}:${sipServerPort}`
    }
    return _camera
}

// function resetDataSource(dataSource, rows) {
//     return dataSource.map(item => {
//         const newSet = new Set()
//         item.catch = new Map()
//         if (item.id === params.id && !item.catch.has(params.id)) {
//             if (item.children) item.children.map(item => newSet.add(item.id))

//             rows.forEach(camera => {
//                 if (!newSet.has(camera.id)) {
//                     const __camera = createCamera(camera)
//                     item.children.push(__camera)
//                 }
//             })

//             item.catch.set(params.id, rows)
//             return item;
//         } else {

//             if (item.children) item.children = resetDataSource(item.children, rows)

//             return item;
//         }
//     })
// }
//
function resetDataSource(dataSource, rows) {
    return dataSource.map(item => {
        if (item.id === params.id) {
            item.children = item.children.filter(item => item.children)
            rows.forEach(camera => {
                item.children.push(createCamera(camera))
            })
            return item;
        } else {
            if (item.children) item.children = resetDataSource(item.children, rows)
            return item;
        }
    })
}

const requestGet = (url) => fetch(url).then(res => res.ok && res.json())

// 获取节点下的设备。
const fetchCamera = () => {
    return requestGet(`${api}?equipmentTypeCode=camera&areaId=${params.id}&onlineStatus=0`).then(res => {
        const { rows } = res.result

        devices.widgetObj._option.dataSource = resetDataSource(dataSource, rows)
        devices.widgetObj.render()
        console.log('正常设备: => ', dataSource, params)
        return dataSource
    })
}

// 获取离线设备
const fetchOfflineCamera = () => {
    return requestGet(`${api}?equipmentTypeCode=camera&onlineStatus=1,2`).then(res => {
        const { rows } = res.result

        devices.widgetObj._option.dataSource = resetDataSource(dataSource, rows)
        devices.widgetObj.render()
        console.log('维修中摄像机: => ', dataSource, params)
        return dataSource
    })
}

// if (params.show && !params.catch?.has(params.id)) {
if (params.show) {
    let url
    if (params.title === '维修中摄像机') {
        fetchOfflineCamera()
    } else {
        fetchCamera().then(() => {
            params = dataSource.find(item => item.id === 'offline')
            fetchOfflineCamera()
        })
    }
    // if (params.show && params.id.indexOf('off') === -1) {
    // `${api}?equipmentCode=camera&areaId=&pageSize=1&pageNum=1`
    // fetch(url).then(res => {
    //     if (res.ok === true) {
    //         return res.json()
    //     }
    // }).then(res => {
    //     const { rows } = res.result

    //     devices.widgetObj._option.dataSource = resetDataSource(dataSource, rows)
    //     devices.widgetObj.render()
    //     console.log('vvvvvvvvvvvvvvvv', dataSource)
    // })
}